package cn.linstudy.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.linstudy.product.entity.AttrAttrgroupRelationEntity;
import cn.linstudy.product.entity.AttrEntity;
import cn.linstudy.product.service.AttrAttrgroupRelationService;
import cn.linstudy.product.service.AttrService;
import cn.linstudy.product.service.CategoryService;
import cn.linstudy.product.vo.AttrGroupRelationVo;
import cn.linstudy.product.vo.AttrGroupWithAttrsVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.linstudy.product.entity.AttrGroupEntity;
import cn.linstudy.product.service.AttrGroupService;
import cn.linstudy.common.utils.PageUtils;
import cn.linstudy.common.utils.R;



/**
 * 属性分组
 *
 * @author XiaoLin
 * @email ljp2020vip@163.com
 * @date 2023-01-04 15:50:41
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") String catalogId){
        // 1. 查出当前分类下的所有属性分组
        // 2. 查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> attrGroupWithAttrsVos = attrGroupService.getAttrGroupWithAttrs(catalogId);
        return R.ok().put("data",attrGroupWithAttrsVos);
    }
    /**
     * 新增关联关系
     */
    @RequestMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrAttrgroupRelationService.addAttrRelation(attrGroupRelationVos);
        return R.ok();
    }

    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrGroupId") Long attrGroupId,
                            @RequestParam Map<String, Object> params){
        // 获取还未关联的属性
        PageUtils page = attrService.getNoRelation(params,attrGroupId);
        return R.ok().put("page",page);
    }

    @RequestMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrService.deleteRelation(attrGroupRelationVos);
        return R.ok();
    }


    @GetMapping("/{attrGroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        // 获取当前分组关联的所有属性
        List<AttrEntity> entities = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", entities);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 拼接三级分类的完整路径
        Long catalogId = attrGroup.getCatelogId();
        Long[] categoryPath = categoryService.findCatalogPath(catalogId);
        attrGroup.setCatalogPath(categoryPath);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
